package com.solo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

@WebServlet(name = "ExamServlet", value = "/exam")
public class ExamServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        int score = 0;

        // 单选题判断
        String q1Answer = request.getParameter("q1");
        if ("A".equals(q1Answer)) {
            score += 20;
        }

        // 多选题判断（正确答案为A、B、C）
        String[] q2Answers = request.getParameterValues("q2");
        List<String> correctQ2Answers = Arrays.asList("A", "B", "C");
        if (q2Answers != null && Arrays.asList(q2Answers).containsAll(correctQ2Answers)) {
            score += 30;
        }

        // 填空题判断（答案：懒汉式、饿汉式）
        String q3_1 = request.getParameter("q3_1");
        String q3_2 = request.getParameter("q3_2");
        if (("懒汉式".equals(q3_1) && "饿汉式".equals(q3_2)) ||
                ("饿汉式".equals(q3_1) && "懒汉式".equals(q3_2))) {
            score += 50;
        }

        out.println("<html><body>");
        out.println("<h1>你的考试成绩是：" + score + " 分</h1>");
        out.println("</body></html>");
    }
}